﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UpdateControls.Correspondence;
using UpdateControls.Correspondence.Memory;

namespace SurveySez.Model.UnitTest
{
	[TestClass]
	public class EmceeEditsSurveys
	{
		private Community _community;
		private Emcee _emcee;

        public TestContext TestContext { get; set; }

		[TestInitialize]
		public void Init()
		{
			_community = new Community(new MemoryStorageStrategy())
				.RegisterAssembly(typeof(Emcee));
			_emcee = _community.AddFact(new Emcee());
		}

		[TestMethod]
		public void EmceeCreatesASurvey()
		{
			Survey survey = _emcee.NewSurvey().Survey;

			Survey[] surveys = _emcee.EmceeSurveys.Select(emceeSurvey => emceeSurvey.Survey).ToArray();
			Assert.AreEqual(1, surveys.Length);
			Assert.AreSame(survey, surveys[0]);
		}

		[TestMethod]
		public void QuestionIsInitiallyNull()
		{
			Survey survey = _emcee.NewSurvey().Survey;

			Assert.IsNull(survey.Question);
		}

		[TestMethod]
		public void TwoSurveysAreDifferent()
		{
			Survey survey1 = _emcee.NewSurvey().Survey;
			Survey survey2 = _emcee.NewSurvey().Survey;

			Assert.AreNotSame(survey1, survey2);
		}

		[TestMethod]
		public void EmceeHasTwoSurveys()
		{
			Survey survey1 = _emcee.NewSurvey().Survey;
			Survey survey2 = _emcee.NewSurvey().Survey;

			Survey[] surveys = _emcee.EmceeSurveys.Select(emceeSurvey => emceeSurvey.Survey).Reverse().ToArray();
			Assert.AreEqual(2, surveys.Length);
			Assert.AreSame(survey1, surveys[0]);
			Assert.AreSame(survey2, surveys[1]);
		}

		[TestMethod]
		public void EmceeSetsTheQuestion()
		{
			Survey survey = _emcee.NewSurvey().Survey;
			survey.Question = "Name something blue";

			Assert.AreEqual("Name something blue", survey.Question);
		}

		[TestMethod]
		public void EmceeChangesTheQuestion()
		{
			Survey survey = _emcee.NewSurvey().Survey;
			survey.Question = "Name something blue";
			survey.Question = "Name something green";

			Assert.AreEqual("Name something green", survey.Question);
		}

		[TestMethod]
		public void EmceeAddsAResponse()
		{
			Survey survey = _emcee.NewSurvey().Survey;
			Response response = survey.NewResponse();

			Response[] responses = survey.Responses.ToArray();
			Assert.AreEqual(1, responses.Length);
			Assert.AreSame(response, responses[0]);
		}

		[TestMethod]
		public void ResponsesAreDifferent()
		{
			Survey survey = _emcee.NewSurvey().Survey;
			Response response1 = survey.NewResponse();
			Response response2 = survey.NewResponse();

			Assert.AreNotSame(response1, response2);
		}

		[TestMethod]
		public void QuestionHasTwoResponses()
		{
			Survey survey = _emcee.NewSurvey().Survey;
			Response response1 = survey.NewResponse();
			Response response2 = survey.NewResponse();

			Response[] responses = survey.Responses.Reverse().ToArray();
			Assert.AreEqual(2, responses.Length);
			Assert.AreSame(response1, responses[0]);
			Assert.AreSame(response2, responses[1]);
		}

		[TestMethod]
		public void AnswerIsInitiallyNull()
		{
			Survey survey = _emcee.NewSurvey().Survey;
			Response response = survey.NewResponse();

			Assert.IsNull(response.Answer);
		}

		[TestMethod]
		public void EmceeSetsAnswer()
		{
			Survey survey = _emcee.NewSurvey().Survey;
			Response response = survey.NewResponse();
			response.Answer = "Cheese";

			Assert.AreEqual("Cheese", response.Answer);
		}

		[TestMethod]
		public void EmceeChangesAnswer()
		{
			Survey survey = _emcee.NewSurvey().Survey;
			Response response = survey.NewResponse();
			response.Answer = "Cheese";
			response.Answer = "Blue cheese";

			Assert.AreEqual("Blue cheese", response.Answer);
		}
	}
}
